package leetcode_201_300;

import java.util.ArrayList;
import java.util.List;

public class LeeCode_216 {
    public static void main(String[] args) {
//        System.out.println(combinationSum3(3, 7));
        System.out.println(combinationSum3(9, 45));
    }
    private static List<List<Integer>> combinationSum3(int k, int n) {
        List<List<Integer>> ans = new ArrayList<>();
        dfs(ans, new ArrayList<>(), k, 1, n, 0);
        return ans;
    }
    private static void dfs(List<List<Integer>> ans, List<Integer> list, int k, int start, int n, int sum){
        if (k == 0 && sum == n){
            ans.add(list);
            return;
        }
        if (start > 9 || k < 0)
            return;
        for (int i = start; i <= 9; i++) {
            ArrayList<Integer> newL = new ArrayList<>(list);
            newL.add(i);
            dfs(ans, newL, k - 1, i + 1, n, sum + i);
        }
    }
}
